/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package WaterLevel;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.io.Connector;
import javax.microedition.sensor.*;
import java.io.IOException;
import sUtils.*;
/**
 *
 * @author thomas.s
 */
public class WaterLevel extends MIDlet implements CommandListener {

    private Command exitCommand;
    private Command eichCommand;
    private Display display;
    private WaterCanvas canvas;
    private SensorConnection sensorConnection;
    
    public WaterLevel () {
        Lang.init();
        exitCommand = new Command (Lang.word("Quit","Beenden"), Command.EXIT, 1);
        eichCommand = new Command (Lang.word("Calibrate","Eichen"), Command.OK, 0);

        display = Display.getDisplay (this);
        canvas = new WaterCanvas ();
       // canvas.setFullScreenMode(true);
        SensorInfo[] sensors = SensorManager.findSensors(null,null);
        for ( int i=0; i<sensors.length; i++ )
        {
            if (sensors[i].getQuantity().equalsIgnoreCase("acceleration"))
            {
                try {
                    sensorConnection = (SensorConnection) Connector.open(sensors[i].getUrl());
                    sensorConnection.setDataListener(canvas, 1);
                } catch (IOException e) { e.printStackTrace(); }
                break;
            }
        }


        canvas.addCommand (exitCommand);
        canvas.addCommand (eichCommand);
        canvas.setCommandListener (this);
    }

    /**
     * Create the MiniColor and make it current
     */
    public void startApp () {
        display.setCurrent (canvas);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction (Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp (true);
            display.setCurrent(null);
            notifyDestroyed ();
        }
        if (c == eichCommand)
        {
            canvas.setZero();
        }
    }
}



